﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine;
using PloobsEngine.Cameras;
using PloobsEngine.Input;
using Microsoft.Xna.Framework;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Utils;
using PloobsEngine.Features;
using PloobsEngine.Commands;
using Bnoerj.AI.Steering.Boids;
using PloobsEngine.Draw;
using PloobsEngineDemo3;
using PloobsEngine.Light;
using Bnoerj.AI.Steering.Pedestrian;
using Etapa1.Pedestrian;

namespace Etapa1.Screens
{
    public class BepuSharperSteerScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;                
        ICamera cam;
        FindSpotPlugIn plugin;
        Vector3 pos = new Vector3(100, 3, 100);
        List<Vector3> endsPosition = new List<Vector3>();
        PathManager pm;

        public BepuSharperSteerScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();            
            mundo = new BrutalForceWorld(physicWorld);             
            engine.Game.IsFixedTimeStep = true;
        }
       
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

            Drawing.Draw3dLine(pos, pos + Vector3.Up * 20, Color.Green);
            Drawing.DrawCircleOrDisk(10, Vector3.Up, pos, Color.Green, 100, true, true);

            foreach (var item in endsPosition)
            {
                Drawing.Draw3dLine(item, item + Vector3.Up * 20, Color.Red);
                Drawing.DrawCircleOrDisk(5, Vector3.Up, item, Color.Purple, 100, true, true);
            }

			// invoke selected PlugIn's Update method
            plugin.Update(EngineStuff.Clock.TotalSimulationTime, EngineStuff.Clock.ElapsedSimulationTime);
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);					
        }

        public override void Draw(GameTime gameTime)
        {
            plugin.Redraw(EngineStuff.Clock.TotalSimulationTime, EngineStuff.Clock.ElapsedSimulationTime);
            base.Draw(gameTime);
        }

        public override void LoadContent(EngineStuff es)
        {
            base.LoadContent(es);

            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            Picking pk = new Picking();
            engine.AddComponent(pk);
            
            Drawing.setClearEveryFrame(true);

            pm = new PathManager(new List<Vector3>() { }, 1);
            plugin = new FindSpotPlugIn(mundo, engine,pm);

            //plugin = new Bnoerj.AI.Steering.Pedestrian.PedestrianPlugIn(mundo,engine);
            //plugin = new BoidsPlugIn(mundo,engine);
            plugin.Open();          
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\blue");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(0, -5, 100), 1, new Vector3(800,2,800));
                pi.isMotionLess = true;
                IShader shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
            }                        

            ///Obstaculos
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(0, 1, 0), 1, new Vector3(3, 20, 40));
                pi.isMotionLess = true;
                IShader shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
                pm.AddCircularObstacle(obj4);
            }

            ///Obstaculos
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(-200, 1, 100), 1, new Vector3(30, 20, 3));
                pi.isMotionLess = true;
                IShader shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
                pm.AddCircularObstacle(obj4);
            }

            ///Obstaculos
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(-40, 1, 180), 1, new Vector3(30, 20, 3));
                pi.isMotionLess = true;
                IShader shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
                pm.AddCircularObstacle(obj4);
            }

            ///Obstaculos
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(0, 1, 150), 1, new Vector3(35, 20, 5));
                pi.isMotionLess = true;
                IShader shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
                pm.AddCircularObstacle(obj4);
            }

            ///Obstaculos
            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\block", "..\\Content\\Textures\\white");
                sm.LoadModelo();
                BoxObject pi = new BoxObject(new Vector3(-340, 1, -150), 1, new Vector3(65, 20, 5));
                pi.isMotionLess = true;
                IShader shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj4 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj4);
                pm.AddCircularObstacle(obj4);
            }





            cam = new CameraFirstPerson(true);            
            cam.FarPlane = 2000;

            //LightThrowBepu lt = new LightThrowBepu(mundo, engine);       

            #region LIGHT
            DirectionalLight ld = new DirectionalLight(new Vector3(-1), Color.White);
            DirectionalLight ld2 = new DirectionalLight(new Vector3(-0.3f, -1, -0.3f), Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Down, Color.White);
            ld.LightIntensity = 0.3f;
            ld2.LightIntensity = 0.3f;
            ld3.LightIntensity = 0.3f;            
            mundo.AddLight(ld);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            #endregion            
         
            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);           

            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            mundo.AddCamera(cam);
            this.World = mundo;

            PickingParameters pp = new PickingParameters(cam, mundo, MouseButtons.RightButton);
            pp.Method = SegmentInterceptMethod.ALL;
            PickingRedirectorCallbackObserver prc = new PickingRedirectorCallbackObserver();
            prc.redirectPicking += new RedirectPicking(prc_redirectPicking);
            pp.Observers.Add(prc);
            CommandProcessor.getCommandProcessor().SendCommandSyncronous(pp);      

        }

        void prc_redirectPicking(object obj)
        {
            RayTestInfo ri = obj as RayTestInfo;
            plugin.AddAgente(pos,ri.ImpactPosition);
            endsPosition.Add(ri.ImpactPosition);
        }        

    }
}

